﻿using System;
using System.IO;
using System.Windows.Forms;


namespace _5._3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnBrowseFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择第一个文件",
                Filter = "所有文件|*.*",
                Multiselect = false
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxFile1.Text = openFileDialog.FileName;
                CheckFilesSelected();
            }
        }

        private void btnMergeFiles_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取两个文件的内容
                string content1 = File.ReadAllText(textBoxFile1.Text);
                string content2 = File.ReadAllText(textBoxFile2.Text);

                // 合并内容
                string mergedContent = content1 + Environment.NewLine + content2;

                // 确保Data目录存在
                string dataDir = Path.Combine(Application.StartupPath, "Data");
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }

                // 创建新文件名
                string newFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string newFilePath = Path.Combine(dataDir, newFileName);

                // 写入新文件
                File.WriteAllText(newFilePath, mergedContent);

                // 显示成功消息
                labelStatus.Text = $"文件合并成功！保存到: {newFilePath}";
                labelStatus.ForeColor = System.Drawing.Color.Green;

                // 打开资源管理器并选中文件
                System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{newFilePath}\"");
            }
            catch (Exception ex)
            {
                labelStatus.Text = $"错误: {ex.Message}";
                labelStatus.ForeColor = System.Drawing.Color.Red;
            }
        
        }

        private void btnBrowseFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择第二个文件",
                Filter = "所有文件|*.*",
                Multiselect = false
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxFile2.Text = openFileDialog.FileName;
                CheckFilesSelected();
            }
        }
        private void CheckFilesSelected()
        {
            btnMergeFiles.Enabled = !string.IsNullOrEmpty(textBoxFile1.Text) &&
                                  !string.IsNullOrEmpty(textBoxFile2.Text);
        }
    }
}

